<?php

abstract class Controller_Site extends Controller_Template {

    /**
     * 
     * Az oldal tartalma
     * @var View
     */
    var $content = "";
    protected $auto_template_render = TRUE;

    /**
     * Loads the content [View] object.
     */
    public function before() {
        parent::before();
        if ($this->request->is_ajax() OR !$this->request->is_initial()) {
            $this->auto_template_render = FALSE;
        } else {
            $this->content = View::factory($this->request->controller() . '/' . $this->request->action());
        }
        //$this->content = View::factory($this->request->controller() . '/' . $this->request->action());
        //$this->menu_meetings = ORM::factory('meetings')->order_by('id')->limit(5)->find_all()->as_array();
    }

    /**
     * Assigns the content [View] as the request response.
     */
    public function after() {
        if ($this->hasFlash('notice'))
            $this->template->set('notice', $this->getFlash('notice'));

        if ($this->hasFlash('error'))
            $this->template->set('error', $this->getFlash('error'));

        if ($this->auto_template_render == FALSE) {
            // We have ajax or internal request here
            $this->template = $this->content;
        } else {
            $this->menu_meetings = ORM::factory('meetings')->order_by('sub_date', 'DESC')->find_all()->as_array();
            $this->template->set('content', $this->content);
            $this->template->set('menu_meetings', $this->menu_meetings);
        }

        parent::after();
    }

    protected function setVar($key, $value) {
        $this->content->set($key, $value);
    }

    public function setFlash($key, $value) {
        Session::instance()->set($key, $value);
    }

    public function hasFlash($key) {
        $value = Session::instance()->get($key, null);
        return $value != null;
    }

    public function getFlash($key) {
        return Session::instance()->get_once($key);
    }
}